package com.tbi.algorithm;

import java.util.List;

import com.tbi.algorithm.configuration.IAlgorithmStopper;
import com.tbi.algorithm.configuration.ICrosser;
import com.tbi.algorithm.configuration.IEvaluator;
import com.tbi.algorithm.configuration.IMutator;
import com.tbi.algorithm.configuration.IPopulationCreator;
import com.tbi.algorithm.configuration.ISelector;

public class ConfigurableGenethicAlgorithm extends AbstractGenethicAlgorithm {
	private IAlgorithmStopper stopper;
	private ICrosser crosser;
	private IEvaluator evaluator;
	private IMutator mutator;
	private IPopulationCreator populationCreator;
	private ISelector selector;

	@Override
	protected List<Object> createInitialPopulation(Object obj) {
		return populationCreator.createPopulation(obj);
	}

	@Override
	protected void evaluate(List<Object> population) {
		evaluator.evaluate(population);
	}

	@Override
	protected void selection(List<Object> population) {
		selector.selection(population);
	}

	@Override
	protected void crossover(List<Object> population) {
		crosser.crossOver(population);
	}

	@Override
	protected void mutation(List<Object> population) {
		mutator.mutation(population);
	}

	@Override
	protected boolean stopCondition(List<Object> population, int generation) {
		return stopper.stopCondition(population, generation);
	}
	
	@Override
	public void doIt(Object obj) {
		if(stopper == null){
			throw new IllegalStateException("Algorithm is not prepared! Stopper is null");
		}
		if(crosser == null){
			throw new IllegalStateException("Algorithm is not prepared! Crosser is null");
		}
		if(evaluator == null){
			throw new IllegalStateException("Algorithm is not prepared! Evaluator is null");
		}
		if(mutator == null){
			throw new IllegalStateException("Algorithm is not prepared! Mutator is null");
		}
		if(populationCreator == null){
			throw new IllegalStateException("Algorithm is not prepared! Population creator is null");
		}
		if(selector == null){
			throw new IllegalStateException("Algorithm is not prepared! Selector is null");
		}
		super.doIt(obj);
	}

	public void setStopper(IAlgorithmStopper stopper) {
		this.stopper = stopper;
	}

	public void setCrosser(ICrosser crosser) {
		this.crosser = crosser;
	}

	public void setEvaluator(IEvaluator evaluator) {
		this.evaluator = evaluator;
	}

	public void setMutator(IMutator mutator) {
		this.mutator = mutator;
	}

	public void setPopulationCreator(IPopulationCreator populationCreator) {
		this.populationCreator = populationCreator;
	}

	public void setSelector(ISelector selector) {
		this.selector = selector;
	}
}
